<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */
?>
<div class="g_upload_uploadertoolbox">
	<div class="g_a_bottom">
		<?php echo sprintf($this->TRANSLATOR->widget('guide'), ini_get('upload_max_filesize')); ?>
	</div>

	<div class="g_a_bottom">
		<center><input id="uploadUploaderToolbox_<?php echo $this->uploadElementId; ?>" name="uploadFile" type="file" /></center>
	</div>
	
	<div class="g_a_bottom">
		<?php echo $this->TRANSLATOR->widget('drag_guide'); ?>
	</div>
	
	<div id="uploadUploaderToolboxFiles"><ul></ul></div>
</div>

<script type="text/javascript">
'Gio.Upload.Widgets.UploaderToolbox'.namespace();
	
/**
 * Upload handlers
 */
Gio.Upload.Widgets.UploaderToolbox.handleUploadComplete = function(event, ID, fileObj, response, data) {
	var data = {
					url: $.evalJSON(response).original.url,
					name: fileObj.name,
					size: Gio.Upload.Uploader.formatSize(fileObj.size)
				};
				
	$('<li/>').append('<span>' + data.name + ' - ' + data.size.size + data.size.suffix + '</span>')
		 	  .append($('<br/>'))
		 	  .append($('<input/>').attr('type', 'text').val(data.url))
		 	  .addClass('g_upload_uploadertoolbox_draggable')
		 	  .attr('widget_data', $.toJSON(data))
		 	  .draggable({
				   cursor: 'crosshair',
				   iframeFix: true,
				   helper: 'clone',
			   })
			  .disableSelection()
		 	  .appendTo($('#uploadUploaderToolboxFiles ul'));
};

Gio.Upload.Widgets.UploaderToolbox.handleDrop = function(droppedElement, event, ui) {
	if (!$(ui.helper).hasClass('g_upload_uploadertoolbox_draggable')) {
		return;
	}
	
	var data = $(ui.helper).attr('widget_data');
	data     = $.evalJSON(data);
	
	switch (true) {
		/** 
		 * Textbox, textarea
		 */
		case ($(droppedElement).attr('type') == 'text' || $(droppedElement).attr('type') == 'textarea'):
			$(droppedElement).val($(droppedElement).val() + data.url);
			break;
			
		/** 
		 * WYSIWYG
		 */
		case ($(droppedElement).find('textarea.g_a_ui_editor').length > 0):
			var id      = $(droppedElement).find('textarea.g_a_ui_editor:first').attr('id');
			var factory = new Gio.Core.Editor.Factory();
			factory.factory('<?php echo $this->editor; ?>')
				   .setId(id)
				   .insertHtml('<a target="_blank" href="' + data.url + '" title="' + data.name + '">' + data.name + ' (' + data.size.size + data.size.suffix + ')' + '</a>');
			break;
	}
};

$(document).ready(function() {
	(new Gio.Upload.Uploader('uploadUploaderToolbox_<?php echo $this->uploadElementId; ?>'))
			.setUploadSettings({
				scriptData: {
					mod: MODULE, 
					PHPSESSID: "<?php echo $this->STRING->escape($this->sessionId); ?>",
					thumbnails: 'none'
				},
				uploader: '<?php echo $this->APP_STATIC_SERVER; ?>/modules/upload/js/jquery.uploadify/uploadify.swf',
				script: '<?php echo $this->url('upload_file_upload'); ?>',
				cancelImg: '<?php echo $this->APP_STATIC_SERVER; ?>/modules/upload/js/jquery.uploadify/cancel.png',
				auto: true,
				multi: true,
				/** TODO: Make the file extensions configurable */
				//fileDesc: '*.jpg;*.jpeg;*.png;*.gif',
				//fileExt: '*.jpg;*.jpeg;*.png;*.gif',
				//simUploadLimit: 5,
				//sizeLimit: 1024 * 1024 * 5,
				onComplete: function(event, ID, fileObj, response, data) {
					Gio.Upload.Widgets.UploaderToolbox.handleUploadComplete(event, ID, fileObj, response, data);
				}
			})
			.setMessageSettings({ sticky: true })
			.setButtonLabel('<?php echo $this->TRANSLATOR->translator('helper_uploader_upload_button', 'upload'); ?>')
			.show();
			
	/**
	 * Make the textbox, textarea, WYSIWYG editors droppable
	 */
	$([
		$('input[type="text"], textarea'),
		$('.g_a_ui_editor').parent()
	]).each(function() {
		$(this).droppable({
			drop: function(event, ui) {
				Gio.Core.ToolBox.callDropHandler($(this), 'Gio.Upload.Widgets.UploaderToolbox.handleDrop', event, ui);
			}
		})
	});
});
</script>